﻿using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using Microsoft.Commerce.Proxy.AccountService;

namespace TestWebApplication.CommerceAccount.APIs
{
    public class CommerceAccountGetAccountApi : CommerceAccountApi
    {
        private GetAccountInput input;
        private GetAccountOutput output;

        private CallerInfo callerInfo;
        private string accountId;

        public CommerceAccountGetAccountApi(BasicHttpBinding binding, EndpointAddress address, X509Certificate2 clientCert, string accountId, CallerInfo callerInfo)
            : base(binding, address, clientCert)
        {
            this.accountId = accountId;
            this.callerInfo = callerInfo;
        }

        public CommerceUserAccount[] Accounts;

        protected override AbstractResponse ExecuteInternal(AccountServiceChannel channel)
        {
            this.output = channel.GetAccount(this.input);

            return output;
        }

        public override void PrepareRequestData()
        {
            this.input = new GetAccountInput()
            {
                APIContext = new APIContext()
                {
                    TrackingGuid = this.trackingGuid,
                },
                CallerInfo = this.callerInfo,
                SearchCriteria = new AccountSearchCriteria()
                {
                    AccountId = this.accountId,
                    Identity = this.callerInfo.Requester
                }
            };

            return;
        }

        public override void PrepareResponseData()
        {
            this.Accounts = new CommerceUserAccount[this.output.AccountOutputInfo.Length];
            
            int i =0;
            foreach (AccountInfo accountInfo in this.output.AccountOutputInfo)
            {
                CommerceUserAccount account = new CommerceUserAccount()
                {
                    AccountInfo = accountInfo
                };

                this.Accounts[i] = account;
            }

            return;
        }
    }
}
